package zhcms.cms;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import zhcms.db.DBManager;
import zhcms.db.DbConn;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.GySplit;

public class GyArticle {
	
	public String  articleid;
	public String  ColumnId;
	public String  ColumnName;
	public String  Title;
	public String  AddDate;
	public String  Content;
	public String  KeyWord;
	public String  FromName;
	public String  ViewNum;
	public String  Author;
	public String  TColor;
	
	public String getColumnId() {
		return ColumnId;
	}

	public void setColumnId(String columnId) {
		ColumnId = columnId;
	}

	public String getColumnName() {
		return ColumnName;
	}

	public void setColumnName(String columnName) {
		ColumnName = columnName;
	}

	public String getTitle() {
		return Title;
	}

	public void setTitle(String title) {
		Title = title;
	}

	public String getAddDate() {
		return AddDate;
	}

	public void setAddDate(String addDate) {
		AddDate = addDate;
	}

	public String getContent() {
		return Content;
	}

	public void setContent(String content) {
		Content = content;
	}

	public String getKeyWord() {
		return KeyWord;
	}

	public void setKeyWord(String keyWord) {
		KeyWord = keyWord;
	}

	public String getFromName() {
		return FromName;
	}

	public void setFromName(String fromName) {
		FromName = fromName;
	}

	public String getViewNum() {
		return ViewNum;
	}

	public void setViewNum(String viewNum) {
		ViewNum = viewNum;
	}

	public String getAuthor() {
		return Author;
	}

	public void setAuthor(String author) {
		Author = author;
	}

	public String getTColor() {
		return TColor;
	}

	public void setTColor(String tColor) {
		TColor = tColor;
	}

	public String getIsImg() {
		return IsImg;
	}

	public void setIsImg(String isImg) {
		IsImg = isImg;
	}

	public String getImg() {
		return Img;
	}

	public void setImg(String img) {
		Img = img;
	}

	public String  IsImg;
	public String  Img;
	
	
	public String getArticleid() {
		return articleid;
	}

	public void setArticleid(String articleid) {
		this.articleid = articleid;
	}

	public GyArticle()
	{
		
	}
	
	
	public void getArticle(String articleid)
	{
		//获取一篇文章
		GyArticle gyarticle=new GyArticle();
		ArrayList<HashMap<String, String>> gyA;
		gyA=new ArrayList<HashMap<String, String>>();
		
		gyA=gyarticle.getArticleinfo(articleid);
		for(int i=0;i<gyA.size();i++)
		{
			Map<String,String> iS=new HashMap<String,String>();
			iS =gyA.get(i);
			setColumnId(iS.get("ColumnId"));
			setColumnName(iS.get("ColumnName"));
			setTitle(iS.get("Title"));
			setAddDate(iS.get("AddDate"));
			setContent(iS.get("Content"));
			setKeyWord(iS.get("KeyWord"));
			setFromName(iS.get("FromName"));
			setViewNum(iS.get("ViewNum"));
			setAuthor(iS.get("Author"));
			setTColor(iS.get("TColor"));
			setIsImg(iS.get("IsImg"));
			setImg(iS.get("Img"));
		}
	}
	
	private ArrayList<HashMap<String, String>> getArticleinfo(String articleid)
	{
		String sql="SELECT `Id` , `ColumnId` , `ColumnName` , `Title` , `AddDate` , `Content` , `KeyWord` , `FromName` , `ViewNum` , `Author` , `TColor` , `IsImg` , `Img` FROM `gyhy_content` where IsDel='0' and IsPass='1' and `Id`='"+articleid+"' ";
		//System.out.println(sql);
		return DbConn.executeQuery(sql);
	}
	
	public String getFirstArticle(String articleid)
	{
		String RetrueS="";
		String sql="select * from gyhy_content where IsDel='0' and IsPass='1' and Id<"+articleid+" order by Id desc";
		ArrayList<HashMap<String, String>> gytS=new ArrayList<HashMap<String, String>>();
		gytS=DbConn.executeQuery(sql);
		if(gytS.size()>0)
		{
			Map<String,String> iS=new HashMap<String,String>();
			iS =gytS.get(0);
			String rTitle=iS.get("Title");
			String rId=iS.get("Id");
			RetrueS="<a href=\"/article.jsp?articleid="+rId+"\">"+rTitle+"</a>";
		}
		else
		{
			RetrueS="无";
		}
		return RetrueS;
	}
	
	public String getNextArticle(String articleid)
	{
		String RetrueS="";
		String sql="select * from gyhy_content where IsDel='0' and IsPass='1' and Id>"+articleid+" order by Id asc";
		ArrayList<HashMap<String, String>> gytS=new ArrayList<HashMap<String, String>>();
		gytS=DbConn.executeQuery(sql);
		if(gytS.size()>0)
		{
			Map<String,String> iS=new HashMap<String,String>();
			iS =gytS.get(0);
			String rTitle=iS.get("Title");
			String rId=iS.get("Id");
			RetrueS="<a href=\"/article.jsp?articleid="+rId+"\">"+rTitle+"</a>";
		}
		else
		{
			RetrueS="无";
		}
		return RetrueS;
	}
	
	//相关文章
	public ArrayList<HashMap<String, String>> getCorrList(String articleid,String num)
	{
		ArrayList<HashMap<String, String>> ReturnSA=new ArrayList<HashMap<String, String>>();
		ReturnSA=null;
		String sql="select KeyWord, `ColumnId` from `gyhy_content` where IsDel='0' and IsPass='1' and Id='"+articleid+"'";
		
		String iKeyWord="";
		String iColumnId="0";
		ArrayList<HashMap<String, String>> gytS;
		gytS=new ArrayList<HashMap<String, String>>();
		gytS=DbConn.executeQuery(sql);
		for(int i=0;i<gytS.size();i++)
		{
			Map<String,String> iS=new HashMap<String,String>();
			iS =gytS.get(i);
			iKeyWord=iS.get("KeyWord");
			iColumnId=iS.get("ColumnId");
			if(iKeyWord.equals("")||iKeyWord.length()<=0)
			{
				//没有关键词，随机取SELECT * FROM `gyhy_content` AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM `gyhy_content`)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id ASC LIMIT 5;
				sql="select * from `gyhy_content` where ColumnId='"+iColumnId+"' order by RAND() limit 0,"+num;
			}
			else
			{
				String WSQL="";
				
				GySplit b = new GySplit();
				b.tool(iKeyWord, ",");
				String[] temp_array = b.Show();
				b = null;
				
				for(int j=0;j<temp_array.length;j++)
				{
					WSQL=WSQL+" InStr(keyword,'"+temp_array[j]+"')>0 or ";
				}
				
				sql="select * from `gyhy_content` where IsDel='0' and IsPass='1' And Id<>"+articleid+" and ("+WSQL+" 1=2) AND ColumnId="+iColumnId+" order by id desc  LIMIT 0 , "+num;
			}
			//System.out.println(sql);
			ReturnSA=DbConn.executeQuery(sql);
		}
		return ReturnSA;
	}
	
	public void updateViewNum(String articleid)
	{
		PooledConnection conn = null;
		String sql="UPDATE gyhy_content SET ViewNum = ViewNum+1 WHERE Id='"+articleid+"'";
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sql);
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			conn.close();
		}
	}
	
	public String getColumnLinkNav(String articleid)
	{
		String RetrueS="<a href=\"/\">首页</a>－》";
		
		String sql="select * from gyhy_content where IsDel='0' and IsPass='1' and Id>"+articleid+" ";
		ArrayList<HashMap<String, String>> gytS=new ArrayList<HashMap<String, String>>();
		gytS=DbConn.executeQuery(sql);
		if(gytS.size()>0)
		{
			Map<String,String> iS=new HashMap<String,String>();
			iS =gytS.get(0);
			String ColumnId=iS.get("ColumnId");
			String ColumnName=iS.get("ColumnName");
			RetrueS="<a href=\"/list.jsp?listid="+ColumnId+"\">"+ColumnName+"</a>";
		}
		else
		{
			RetrueS="";
		}
		return RetrueS;
	}
	

}
